<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-select
        v-model="dataForm.status"
        filterable
        placeholder="状态"
        @change="selectStatus"
      >
        <el-option
          v-for="item in options"
          :key="item.status"
          :label="item.label"
          :value="item.status"
        >
        </el-option>
      </el-select>
      <el-select
        v-model="dataForm.free"
        filterable
        placeholder="是否免费"
        @change="selectFree"
      >
        <el-option
          v-for="item in optionsFree"
          :key="item.status"
          :label="item.label"
          :value="item.status"
        >
        </el-option>
      </el-select>
      <el-form-item>
        <el-input
          v-model="dataForm.key"
          placeholder="请输入主题名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-if="isAuth('theme:theme-list:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        :index="indexMethod"
        label="#"
        prop="Serial_number"
        sortable
        type="index"
      >
      </el-table-column>
      <el-table-column
        prop="useNickname"
        header-align="center"
        align="center"
        label="设计师"
      >
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="主题名"
      >
      </el-table-column>
      <el-table-column
        prop="label"
        header-align="center"
        align="center"
        label="标签"
      >
        <template slot-scope="scope">
          <el-tag
            :key="tag"
            v-for="tag in scope.row.labels"
            :disable-transitions="false"
          >
            {{ tag }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="thumb"
        header-align="center"
        align="center"
        label="缩略图"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 90px; height: 60px"
            :src="scope.row.thumb + '?x-oss-process=image/resize,h_100,w_100'"
            fit="contain"
            @click="onPreview(scope.row.thumb)"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="fileName"
        header-align="center"
        align="center"
        label="附件名称"
      >
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="price"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        :formatter="handlderPrice"-->
      <!--        label="价格">-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="classifyName"
        header-align="center"
        align="center"
        label="分类名称"
      >
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="describes"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        width="200"-->
      <!--        :formatter="handlderDescribes"-->
      <!--        label="描述">-->
      <!--      </el-table-column>-->
      <el-table-column label="描述" header-align="center" style="width: 30%;">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            title="描述"
            width="500"
            trigger="hover"
          >
            <div>{{ scope.row.describes }}</div>
            <span slot="reference">{{
              scope.row.describes.substr(0, 25) + "..."
            }}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column width="80" label="是否免费">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.free === 1">免费</el-tag>
          <el-tag type="danger" v-else-if="scope.row.free === 2"
            >VIP+广告</el-tag
          >
          <el-tag type="danger" v-else-if="scope.row.free === 3">仅Vip</el-tag>
          <el-tag type="danger" v-else-if="scope.row.free === 4">仅广告</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="100" label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">已上架</el-tag>
          <el-tag type="warning" v-else-if="scope.row.status === 2"
            >待上架</el-tag
          >
          <el-tag type="danger" v-else-if="scope.row.status === 3"
            >已删除</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="updated"
        header-align="center"
        align="center"
        label="更新时间"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="160"
        label="操作"
      >
        <template slot-scope="scope">
          <!--          <template v-if="scope.row.status==1">-->
          <!--            <el-button type="text" size="small" @click="itemSoldOut(scope.row.id)">下架</el-button>-->
          <!--          </template>-->
          <!--          <template v-if="scope.row.status==2">-->
          <!--            <el-button type="text" size="small" @click="itemPutaWay(scope.row.id)">待上架</el-button>-->
          <!--          </template>-->
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <el-image-viewer
      v-if="showViewer"
      :on-close="closeViewer"
      :url-list="[src]"
    />
  </div>
</template>

<script>
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
export default {
  data() {
    return {
      options: [
        {
          status: "",
          label: "全部状态"
        },
        {
          status: "1",
          label: "已上架"
        },
        {
          status: "2",
          label: "已下架"
        }
      ],
      optionsFree: [
        {
          status: "",
          label: "全部状态"
        },
        {
          status: "1",
          label: "免费"
        },
        {
          status: 2,
          label: "VIP+视频"
        },
        {
          status: 3,
          label: "仅VIP"
        },
        {
          status: 4,
          label: "仅视频"
        }
      ],
      dataForm: {
        free: "",
        status: "",
        key: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      showViewer: false,
      src: ""
    };
  },
  components: {
    ElImageViewer
  },
  activated() {
    this.getDataList();
  },
  methods: {
    indexMethod(index) {
      return index + 1;
    },
    onPreview(url) {
      this.src = url;
      this.showViewer = true;
    },
    closeViewer() {
      this.showViewer = false;
    },
    handlderPrice(row, column) {
      var value = this.regFenToYuan(row.price);
      return value + " 元";
    },
    selectStatus(value) {
      this.dataForm.status = value;
      this.getDataList();
    },
    selectFree(value) {
      this.dataForm.free = value;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/theme/theme-list/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
          free: this.dataForm.free,
          status: this.dataForm.status
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    //格式化描述
    handlderDescribes(row, column) {
      if (row.describes === null || row.describes === "") {
        return "-";
      }
      var len = row.describes.length;
      if (len >= 13) {
        var str = row.describes.slice(0, 13);
        return str + "...";
      }
      return row.describes;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/theme/theme-list/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 商品下架
    itemSoldOut(id) {
      this.$confirm(`确定要对该主题进行下架操作操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/theme/theme-list/update"),
          method: "post",
          data: this.$http.adornData({
            id: id,
            status: 2
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 商品重新上架
    itemPutaWay(id) {
      this.$confirm(`确定要对该主题进行上架操作操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/theme/theme-list/update"),
          method: "post",
          data: this.$http.adornData({
            id: id,
            status: 1
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    regFenToYuan(fen) {
      var num = fen;
      num = fen * 0.01;
      num += "";
      var reg =
        num.indexOf(".") > -1
          ? /(\d{1,3})(?=(?:\d{3})+\.)/g
          : /(\d{1,3})(?=(?:\d{3})+$)/g;
      num = num.replace(reg, "$1");
      num = this.toDecimal2(num);
      return num;
    },
    toDecimal2(x) {
      var f = parseFloat(x);
      if (isNaN(f)) {
        return false;
      }
      var f = Math.round(x * 100) / 100;
      var s = f.toString();
      var rs = s.indexOf(".");
      if (rs < 0) {
        rs = s.length;
        s += ".";
      }
      while (s.length <= rs + 2) {
        s += "0";
      }
      return s;
    }
  }
};
</script>

<style>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
